package com.crimsec.utils.snmp.model;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 * $Id$
 *
 */
@Entity
@Table(name = "event")
@XmlRootElement
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@NamedQueries({
    @NamedQuery(name = "Event.findAll", query = "SELECT e FROM Event e"),
    @NamedQuery(name = "Event.findById", query = "SELECT e FROM Event e WHERE e.id = :id"),
    @NamedQuery(name = "Event.findByReqtime", query = "SELECT e FROM Event e WHERE e.reqtime = :reqtime"),
    @NamedQuery(name = "Event.findByRestime", query = "SELECT e FROM Event e WHERE e.restime = :restime"),
    @NamedQuery(name = "Event.findByHost", query = "SELECT e FROM Event e WHERE e.host = :host"),
    @NamedQuery(name = "Event.findByConnectstr", query = "SELECT e FROM Event e WHERE e.connectstr = :connectstr")})
@SequenceGenerator(name = "seq_event", sequenceName = "seq_event", allocationSize = 1)
public class Event extends AbstractEntity implements Serializable {

    private static final long serialVersionUID = 1L;
    //   
    @Id //@XmlAttribute
    @Basic(optional = false)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_event")
    @Column(name = "id")
    private Long id;
    //время запроса
    @Column(name = "reqtime")
    @Temporal(TemporalType.TIMESTAMP)
    private Date reqtime;
    //время ответа
    @Column(name = "restime")
    @Temporal(TemporalType.TIMESTAMP)
    private Date restime;
    //имя хоста агента
    @Column(name = "hoststr")
    private String host;
    //строка соединения
    @Column(name = "connectstr")
    private String connectstr;
    //ссылка на тип события see also(get|set)Evtype
    @Column(name = "typeev_id")
    private Short typeevId;
    //данные события
    @OneToMany(mappedBy = "event", cascade = CascadeType.ALL)
    private Collection<Eventdata> eventdata;// @XmlTransient

    @XmlTransient
    public Collection<Eventdata> getEventdata() {
        return eventdata;
    }

    public void setEventdata(Collection<Eventdata> eventdata) {
        this.eventdata = eventdata;
    }

    public Event() {
    }

    public Event(Date reqtime, Date restime, String host, String connect, Evtype evtype) {
        this.reqtime = reqtime;
        this.restime = restime;
        this.host = host;
        this.connectstr = connect;
        setEvtype(evtype);
    }

    @XmlAttribute
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Date getReqtime() {
        return reqtime;
    }

    public void setReqtime(Date reqtime) {
        this.reqtime = reqtime;
    }

    public Date getRestime() {
        return restime;
    }

    public void setRestime(Date restime) {
        this.restime = restime;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getConnectstr() {
        return connectstr;
    }

    public void setConnectstr(String connectstr) {
        this.connectstr = connectstr;
    }

    public Evtype getEvtype() {
        return Evtype.parse(typeevId);
    }

    public final void setEvtype(Evtype typeev) {
        this.typeevId = (short) typeev.getValue();
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Event)) {
            return false;
        }
        Event other = (Event) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.crimsec.utils.snmp.model.Event[ id=" + id
                + ", reqtime=" + reqtime
                + ", restime=" + restime
                + ", hoststr=" + host
                + ", connectstr=" + connectstr
                + ", evtype=" + this.getEvtype()
                + " ]";
    }
}
